package com.cskaoyan.controller;

import com.cskaoyan.bean.AdminInfoBean;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.InfoData;
import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.administrators.Admin;
import com.cskaoyan.config.shiro.MallToken;
import com.cskaoyan.service.AdministratorsService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("admin/auth")
public class AuthController {

    @Autowired
    AdministratorsService administratorsService;

    @SuppressWarnings("all")
    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        //用type区分微信端和admin端
        MallToken token = new MallToken(username, password, "admin");
        //执行登陆的方法
        String usernames = (String) subject.getPrincipal();
        Admin admin = administratorsService.getAdmin(username);

        Date lastLoginTime = admin.getLastLoginTime();

        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            administratorsService.logWriter(BaseRespVo.ok(), username, lastLoginTime, 1, "登录",false,"用户名错误",null);
            return BaseRespVo.fail("用户名错误");
        } catch (IncorrectCredentialsException e) {
            administratorsService.logWriter(BaseRespVo.ok(), username, lastLoginTime, 1, "登录",false,"密码错误",null);
            return BaseRespVo.fail("密码错误");
        }
        Serializable id = subject.getSession().getId();

        LoginUserData loginUserData = new LoginUserData();
        loginUserData.setAdminInfo(new AdminInfoBean(admin));

        loginUserData.setToken((String) id);

        administratorsService.logWriter(BaseRespVo.ok(), username, lastLoginTime, 1, "登录",true,null,null);
        return BaseRespVo.ok(loginUserData);
    }

    @RequestMapping("info")
    public BaseRespVo info() throws JsonProcessingException {
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipals().getPrimaryPrincipal();
        InfoData infoData = new InfoData();
        Admin admin = administratorsService.getAdmin(username);
        infoData.setName(admin.getUsername());
        infoData.setAvatar(admin.getAvatar());
        List<String> roles = administratorsService.getRoles(admin);
        infoData.setRoles(roles);
        List<String> perms = administratorsService.getPerms(admin);
        infoData.setPerms(perms);
        return BaseRespVo.ok(infoData);

    }

    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        if(subject != null) {
            String username = (String) subject.getPrincipal();
            Admin admin = administratorsService.getAdmin(username);
            Date lastLoginTime = admin.getLastLoginTime();
            administratorsService.logWriter(BaseRespVo.ok(), username, lastLoginTime, 1, "退出",true,null,null);
            subject.logout();
        }

        return BaseRespVo.ok("成功！");
    }
}
